// AWTDialog.cpp: implementation of the AWTDialog class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "AWTDialog.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTDialog, AWTWindow)

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

AWTDialog::AWTDialog(AWTFrame* parent) : AWTWindow(parent)
{
	construct(parent, CString(), FALSE);
}

AWTDialog::AWTDialog(AWTFrame* parent, BOOL modal) : AWTWindow(parent)
{
	construct(parent, CString(), modal);
}

AWTDialog::AWTDialog(AWTFrame* parent, LPTSTR title) : AWTWindow(parent)
{
	construct(parent, CString(title), FALSE);
}

AWTDialog::AWTDialog(AWTFrame* parent, CString& title) : AWTWindow(parent)
{
	construct(parent, title, FALSE);
}

AWTDialog::AWTDialog(AWTFrame* parent, CString& title, BOOL modal) : AWTWindow(parent)
{
	construct(parent, title, modal);
}
/*
AWTDialog::AWTDialog(CWnd* parent, CString& title, BOOL modal) : AWTWindow(parent)
{
	construct(parent, title, modal);
}
*/
AWTDialog::~AWTDialog()
{

}

void AWTDialog::setTitle(CString& title)
{
	m_title = title;
	AWTDialogPeer* peer = (AWTDialogPeer*)m_peer;
	if (m_peer != NULL) 
	{
		peer->setTitle(title);
	}
}

CWnd* AWTDialog::createComponent()
{
	AWTFrame* parent = (AWTFrame*)getParent();
	CWnd* wParent = parent->getPeer();
	if (wParent == NULL)
		return wParent;
	CString cls = parent->getDlgWndClass();
	CWnd* pwnd = new AWTDialogPeer(this);
	CRect rc = getBounds();
	pwnd->CreateEx(WS_EX_DLGMODALFRAME, cls, m_title, WS_POPUP | WS_CAPTION | WS_SYSMENU, 
		rc.left, rc.top, rc.Width(), rc.Height(), wParent->m_hWnd, 0, NULL);
	return pwnd;
}

void AWTDialog::reshape(int x, int y, int w, int h)
{
	m_x = x;
	m_y = y;
	m_w = w;
	m_h = h;
	if (m_peer != NULL)
	{
		m_peer->MoveWindow(m_x,m_y,
			m_w + (::GetSystemMetrics(SM_CXDLGFRAME) * 2),
			m_h+ (::GetSystemMetrics(SM_CYDLGFRAME) * 2) + ::GetSystemMetrics(SM_CYCAPTION));
	}
}

void AWTDialog::pack() 
{
	AWTWindow::pack();
	CWnd* w = AfxGetMainWnd();
	if (!w->IsWindowVisible() || w->IsIconic())
		m_peer->CenterWindow(CWnd::GetDesktopWindow());
	else
		m_peer->CenterWindow(m_parent->getPeer());
	m_peer->SetForegroundWindow();
}

void AWTDialog::show() 
{
	AWTWindow::show();
	CWnd* w = AfxGetMainWnd();
	if (m_modal)
	{
		m_parent->setEnabled(FALSE);
		w->EnableWindow(FALSE);
		setEnabled(TRUE);
		m_peer->RunModalLoop(MLF_NOIDLEMSG | MLF_NOKICKIDLE);
		dispose();
		w->EnableWindow(TRUE);
		m_parent->setEnabled(TRUE);
		m_parent->requestFocus();
	}
	else
	{
		m_parent->setEnabled(FALSE);
		w->EnableWindow(FALSE);
		setEnabled(TRUE);
	}
}

void AWTDialog::dispose()
{
	CWnd* w = AfxGetMainWnd();
	if (m_modal)
	{
		if (m_peer != NULL)
			m_peer->PostMessage(WM_SYSCOMMAND, SC_CLOSE, 0L);
	}
	else
	{
		w->EnableWindow(TRUE);
		m_parent->setEnabled(TRUE);
		AWTWindow::dispose();
		m_parent->requestFocus();
	}
}
	
